package com.xuemeng.web;


import com.github.pagehelper.PageHelper;
import com.xuemeng.entity.Tdept;
import com.xuemeng.entity.Trole;
import com.xuemeng.entity.Tuser;
import com.xuemeng.entity.Tuserrole;
import com.xuemeng.model.JqgridBean;
import com.xuemeng.model.PageRusult;
import com.xuemeng.service.TdeptService;
import com.xuemeng.service.TroleService;
import com.xuemeng.service.TuserService;
import com.xuemeng.service.TuserroleService;
import com.xuemeng.util.Execl;
import com.xuemeng.util.MD5Util;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.security.provider.MD5;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


/**
 * 后台管理用户Controller
 *
 * @author xuemeng
 */
@Controller
@RequestMapping("/admin/user")
public class UserAdminController {

        @Resource
        private TuserService userService;
        @Resource
        private TdeptService deptService;

        @Resource
        private TroleService roleService;

        @Resource
        private TuserroleService userRoleService;


        @RequestMapping("/tousermanage")
        @RequiresPermissions(value = {"用户管理"})
        public String tousermanage() {
                return "power/user";
        }

        /**
         * 分页查询用户信息
         */
        @ResponseBody
        @RequestMapping(value = "/list")
        @RequiresPermissions(value = {"用户管理"})
        public Map<String, Object> list(JqgridBean jqgridbean
                        /*String userName,@RequestParam(value="page",required=false)Integer page*/) throws Exception {
                LinkedHashMap<String, Object> resultmap = new LinkedHashMap<String, Object>();
                LinkedHashMap<String, Object> datamap = new LinkedHashMap<String, Object>();

                Example tuserExample = new Example(Tuser.class);
                //tuserExample.or().andIdNotEqualTo(1L);
                Example.Criteria criteria = tuserExample.or();
                criteria.andNotEqualTo("userName", "admin");

                if (StringUtils.isNotEmpty(jqgridbean.getSearchField())) {
                        if ("userName".equalsIgnoreCase(jqgridbean.getSearchField())) {
                                        criteria.andLike("userName", "%"+jqgridbean.getSearchString()+"%");
                        }
                }

                if (StringUtils.isNotEmpty(jqgridbean.getSidx()) && StringUtils.isNotEmpty(jqgridbean.getSord())) {
                        tuserExample.setOrderByClause(jqgridbean.getSidx() + " " + jqgridbean.getSord());
                }

                PageHelper.startPage(jqgridbean.getPage(), jqgridbean.getLength());
                List<Tuser> userList = userService.selectByExample(tuserExample);
                PageRusult<Tuser> pageRusult = new PageRusult<Tuser>(userList);


       /* Integer totalrecords = userService.selectCountByExample(tuserExample);//总记录数
        Page pagebean = new Page(jqgridbean.getLength() * ((jqgridbean.getPage() > 0 ? jqgridbean.getPage() : 1) - 1), jqgridbean.getLength(), totalrecords);
        tuserExample.setPage(pagebean);
        tuserExample.setOrderByClause(jqgridbean.getSidx() + " " + jqgridbean.getSord());
        List<Tuser> userList = userService.selectByExample(tuserExample);*/


                for (Tuser u : userList) {
                        List<Trole> roleList = roleService.selectRolesByUserId(u.getId());
                        StringBuffer sb = new StringBuffer();
                        for (Trole r : roleList) {
                                sb.append("," + r.getName());
                        }
                        u.setRoles(sb.toString().replaceFirst(",", ""));
                }

                resultmap.put("currpage", String.valueOf(pageRusult.getPageNum()));
                resultmap.put("totalpages", String.valueOf(pageRusult.getPages()));
                resultmap.put("totalrecords", String.valueOf(pageRusult.getTotal()));
                resultmap.put("datamap", userList);

                return resultmap;
        }


        @ResponseBody
        @RequestMapping(value = "/addupdateuser")
        @RequiresPermissions(value = {"用户管理"})
        public Map<String, Object> addupdateuser(Tuser tuser) {
                LinkedHashMap<String, Object> resultmap = new LinkedHashMap<String, Object>();
                try {
                        if (tuser.getId() == null) {//新建
                                //首先判断用户名是否可用
                                Example tuserExample = new Example(Tuser.class);
                                tuserExample.or().andEqualTo("userName", tuser.getUserName());
                                List<Tuser> userlist = userService.selectByExample(tuserExample);
                                if (userlist != null && userlist.size() > 0) {
                                        resultmap.put("state", "fail");
                                        resultmap.put("mesg", "当前用户名已存在");
                                        return resultmap;
                                }
                                //设置MD5
                                tuser.setPassword(MD5Util.encrypt(tuser.getPassword()));
                                userService.saveNotNull(tuser);
                        } else {//编辑
                                Tuser oldObject = userService.selectByKey(tuser.getId());
                                if (oldObject == null) {
                                        resultmap.put("state", "fail");
                                        resultmap.put("mesg", "当前用户名不存在");
                                        return resultmap;
                                } else {
                                        tuser.setPassword(oldObject.getPassword());
                                        userService.updateNotNull(tuser);
                                }
                        }
                        resultmap.put("state", "success");
                        resultmap.put("mesg", "操作成功");
                        return resultmap;
                } catch (Exception e) {
                        e.printStackTrace();
                        resultmap.put("state", "fail");
                        resultmap.put("mesg", "操作失败，系统异常");
                        return resultmap;
                }
        }



        @ResponseBody
        @RequestMapping(value = "/deleteuser")
        @RequiresPermissions(value = {"用户管理"})
        public Map<String, Object> deleteuser(Tuser tuser) {
                LinkedHashMap<String, Object> resultmap = new LinkedHashMap<String, Object>();
                try {
                        if (tuser.getId() != null && !tuser.getId().equals(0)) {
                                Tuser user = userService.selectByKey(tuser.getId());
                                if (user == null) {
                                        resultmap.put("state", "fail");
                                        resultmap.put("mesg", "删除失败,无法找到该记录");
                                        return resultmap;
                                } else {

                                        //还需删除用户角色中间表
                                        Example tuserroleexample = new Example(Tuserrole.class);
                                        tuserroleexample.or().andEqualTo("userId", tuser.getId());
                                        userRoleService.deleteByExample(tuserroleexample);

                                        userService.delete(tuser.getId());

                                }
                        } else {
                                resultmap.put("state", "fail");
                                resultmap.put("mesg", "删除失败");
                        }


                        resultmap.put("state", "success");
                        resultmap.put("mesg", "删除成功");
                        return resultmap;
                } catch (Exception e) {
                        e.printStackTrace();
                        resultmap.put("state", "fail");
                        resultmap.put("mesg", "删除失败，系统异常");
                        return resultmap;
                }
        }




        @ResponseBody
        @RequestMapping(value = "/selectUserById")
        @RequiresPermissions(value = {"用户管理"})
        public Map<String, Object> selectUserById(Tuser tuser) {
                LinkedHashMap<String, Object> resultmap = new LinkedHashMap<String, Object>();
                try {
                        if (tuser.getId() != null && !tuser.getId().equals(0)) {
                                tuser = userService.selectByKey(tuser.getId());
                                if (tuser == null) {
                                        resultmap.put("state", "fail");
                                        resultmap.put("mesg", "无法找到该记录");
                                        return resultmap;
                                }
                        } else {
                                resultmap.put("state", "fail");
                                resultmap.put("mesg", "无法找到该记录的id");
                                return resultmap;
                        }

                        List<Trole> roleList = roleService.selectRolesByUserId(tuser.getId());
                        StringBuffer sb = new StringBuffer();
                        for (Trole r : roleList) {
                                sb.append("," + r.getName());
                        }
                        tuser.setRoles(sb.toString().replaceFirst(",", ""));



                        //所有角色
                        Example troleExample = new Example(Trole.class);
                        //troleExample.or().andNameNotEqualTo("管理员");
                        List<Trole> allrolelist = roleService.selectByExample(troleExample);

                        resultmap.put("roleList", roleList);//用户拥有的所有角色


                        Iterator<Trole> it = allrolelist.iterator();
                        while (it.hasNext()) {
                                Trole temp = it.next();
                                for (Trole e2 : roleList) {
                                        if (temp.getId().compareTo(e2.getId()) == 0) {
                                                it.remove();
                                        }
                                }
                        }

                        List<Trole> notinrolelist = allrolelist;
                        //获取部门信息
                        Example deptExample = new Example(Tdept.class);
                        //troleExample.or().andNameNotEqualTo("管理员");
                        List<Tdept> deptList = deptService.selectByExample(deptExample);
                        resultmap.put("deptList",deptList);//部门列表
                        resultmap.put("notinrolelist", notinrolelist);//用户不拥有的角色

                        resultmap.put("tuser", tuser);
                        resultmap.put("state", "success");
                        resultmap.put("mesg", "获取成功");
                        return resultmap;
                } catch (Exception e) {
                        e.printStackTrace();
                        resultmap.put("state", "fail");
                        resultmap.put("mesg", "获取失败，系统异常");
                        return resultmap;
                }
        }

        @ResponseBody
        @RequestMapping(value = "/getDeptList")
        @RequiresPermissions(value = {"用户管理"})
        public Map<String, Object> getDeptList() {
                LinkedHashMap<String, Object> resultmap = new LinkedHashMap<String, Object>();
                try {
                        //获取部门信息
                        Example deptExample = new Example(Tdept.class);
                        List<Tdept> deptList = deptService.selectByExample(deptExample);
                        resultmap.put("deptList",deptList);//部门列表
                        resultmap.put("state", "success");
                        resultmap.put("mesg", "获取成功");
                        return resultmap;
                } catch (Exception e) {
                        e.printStackTrace();
                        resultmap.put("state", "fail");
                        resultmap.put("mesg", "获取失败，系统异常");
                        return resultmap;
                }
        }



        //设置用户角色
        @ResponseBody
        @RequestMapping(value = "/saveRoleSet")
        @RequiresPermissions(value = {"用户管理"})
        public Map<String, Object> saveRoleSet(Integer[] role, Integer id) {
                LinkedHashMap<String, Object> resultmap = new LinkedHashMap<String, Object>();
                try {
                        // 根据用户id删除所有用户角色关联实体
                        Example tuserroleexample = new Example(Tuserrole.class);
                        tuserroleexample.or().andEqualTo("userId", id);
                        userRoleService.deleteByExample(tuserroleexample);

                        if (role != null && role.length > 0) {
                                for (Integer roleid : role) {
                                        Tuserrole tuserrole = new Tuserrole();
                                        tuserrole.setRoleId(roleid);
                                        tuserrole.setUserId(id);
                                        userRoleService.saveNotNull(tuserrole);
                                }
                        }

                        resultmap.put("state", "success");
                        resultmap.put("mesg", "设置成功");
                        return resultmap;
                } catch (Exception e) {
                        e.printStackTrace();
                        resultmap.put("state", "fail");
                        resultmap.put("mesg", "设置失败，系统异常");
                        return resultmap;
                }
        }




        /**
         * 安全退出
         *
         * @return
         * @throws Exception
         */

        @GetMapping("/logout")
        @RequiresPermissions(value = {"安全退出"})
        public String logout() throws Exception {
//		logService.save(new Log(Log.LOGOUT_ACTION,"用户注销"));
                SecurityUtils.getSubject().logout();
                return "redirect:/tologin";
        }



        //跳转到修改密码页面
        @RequestMapping("/toUpdatePassword")
        @RequiresPermissions(value = {"修改密码"})
        public String toUpdatePassword() {
                return "power/updatePassword";
        }




        //修改密码
        @ResponseBody
        @PostMapping("/updatePassword")
        @RequiresPermissions(value = {"修改密码"})
        public Map<String, Object> updatePassword(Tuser tuser) throws Exception {
                LinkedHashMap<String, Object> resultmap = new LinkedHashMap<String, Object>();
                try {

                        if (tuser == null) {
                                resultmap.put("state", "fail");
                                resultmap.put("mesg", "设置失败，缺乏字段信息");
                                return resultmap;
                        } else {
                                if (tuser.getId() != null && tuser.getId().intValue() != 0 && StringUtils.isNotEmpty(tuser.getUserName()) && StringUtils.isNotEmpty(tuser.getOldPassword()) && StringUtils.isNotEmpty(tuser.getPassword())) {
                                        Example userExample = new Example(Tuser.class);
                                        Example.Criteria criteria = userExample.or();
                                        criteria.andEqualTo("id", tuser.getId()).andEqualTo("userName", tuser.getUserName()).andEqualTo("password",MD5Util.encrypt(tuser.getOldPassword()));
                                        List<Tuser> tuserList = userService.selectByExample(userExample);
                                        if (tuserList == null || tuserList.size() == 0) {
                                                resultmap.put("state", "fail");
                                                resultmap.put("mesg", "用户名或密码错误");
                                                return resultmap;
                                        } else {
                                                Tuser newEntity = tuserList.get(0);
                                                newEntity.setPassword(MD5Util.encrypt(tuser.getPassword()));
                                                userService.updateNotNull(newEntity);
                                        }
                                } else {
                                        resultmap.put("state", "fail");
                                        resultmap.put("mesg", "设置失败，缺乏字段信息");
                                        return resultmap;
                                }
                        }

                        resultmap.put("state", "success");
                        resultmap.put("mesg", "密码修改成功");
                        return resultmap;
                } catch (Exception e) {
                        e.printStackTrace();
                        resultmap.put("state", "fail");
                        resultmap.put("mesg", "密码修改失败，系统异常");
                        return resultmap;
                }
        }



        /**
         * 导出user信息到execl
         */
        @ResponseBody
        @PostMapping("/export")
        @RequiresPermissions(value = {"导出execl"})
        public Map<String, Object> export(HttpServletResponse response) {
                LinkedHashMap<String, Object> resultmap = new LinkedHashMap<String, Object>();
                Example tuserExample = new Example(Tuser.class);
                List<Tuser> userlist = userService.selectByExample(tuserExample);
                if (userlist.size() < 1) {
                        resultmap.put("state", "fail");
                        resultmap.put("mesg", "没有错查询到数据，查询数据出错啦");
                } else {
                        try {
                                Execl.exportExcel(userlist, "用户信息表", "部门用户", Tuser.class, "用户信息表.xls", response);
                                resultmap.put("state", "success");
                                resultmap.put("mesg", "导出成功");
                                response.setStatus(200);
                        } catch (Exception e) {
                                e.printStackTrace();
                                resultmap.put("state", "fail");
                                resultmap.put("mesg", "导出失败");
                                response.setStatus(500);

                        }

                }


                return resultmap;
        }

        /**
         * 导入user信息从execl
         */
        @ResponseBody
        @PostMapping("/import")
        @RequiresPermissions(value = {"导入execl"})
        public void importExecl() {
                String filePath = "F:\\海贼王.xls";
                //解析excel，
                List<Tuser> personList = Execl.importExcel(filePath, 1, 1, Tuser.class);
                //也可以使用MultipartFile,使用 FileUtil.importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass)导入
                System.out.println("导入数据一共【" + personList.size() + "】行");




        }




}
